import os
import random
import time

import requests
#
# import pygame
#
# def playMusic(filename, loops=0, start=0.0, value=0.5):
#     """
#     :param filename: 文件名
#     :param loops: 循环次数
#     :param start: 从多少秒开始播放
#     :param value: 设置播放的音量，音量value的范围为0.0到1.0
#     :return:
#     """
#     flag = False  # 是否播放过
#     pygame.mixer.init()  # 音乐模块初始化
#     while 1:
#         if flag == 0:
#             pygame.mixer.music.load(filename)
#             # pygame.mixer.music.play(loops=0, start=0.0) loops和start分别代表重复的次数和开始播放的位置。
#             pygame.mixer.music.play(loops=loops, start=start)
#             pygame.mixer.music.set_volume(value)  # 来设置播放的音量，音量value的范围为0.0到1.0。
#         if pygame.mixer.music.get_busy() == True:
#             flag = True
#         else:
#             if flag:
#                 pygame.mixer.music.stop()  # 停止播放
#                 break
#



def remove_salt(frame, src, color, target, neighbour_count):
    '''
    去除孤立点，去除椒盐噪声
    :param frame: 原图
    :param src:要处理的点颜色
    :param color:如果周围都是这个颜色
    :param target:如果周围都是这个颜色，就把src 变成target的颜色
    :param neighbour_count:周围的 color 数量为大约等于 neighbour_count，就把这个像素变成 target
    :return:
    '''
    # print(frame.shape)  # shape内包含三个元素：按顺序为高、宽、通道数
    height = frame.shape[0]
    width = frame.shape[1]

    for row in range(height):  # 遍历高
        for col in range(width):  # 遍历宽

            try:
                sum_count = 0

                top_left = frame[row - 1, col - 1]
                top = frame[row - 1, col]
                top_right = frame[row - 1, col + 1]

                left = frame[row, col - 1]
                right = frame[row, col + 1]

                bottom_left = frame[row + 1, col - 1]
                bottom = frame[row + 1, col]
                bottom_right = frame[row + 1, col + 1]

                center = frame[row, col]

                if top_left == color:
                    sum_count = sum_count + 1

                if top == color:
                    sum_count = sum_count + 1

                if top_right == color:
                    sum_count = sum_count + 1

                if left == color:
                    sum_count = sum_count + 1

                if right == color:
                    sum_count = sum_count + 1

                if bottom_left == color:
                    sum_count = sum_count + 1

                if bottom == color:
                    sum_count = sum_count + 1

                if bottom_right == color:
                    sum_count = sum_count + 1

                if frame[row, col] == src and sum_count >= neighbour_count:
                    frame[row, col] = target
            except Exception:
                frame[row, col] = target
    return frame



nameSet = set()
def getRandomInt():
    '''
    获取随机数
    :return:
    '''
    ret = random.randint(1, 100000000)
    if not nameSet.__contains__(ret):
        nameSet.add(ret)
        return ret
    else:
        getRandomInt()


def collectData(webName, link):
    '''
    收集某网站的验证码
    :param webName:
    :param link:
    :return:
    '''
    if not os.path.exists(webName):
        os.mkdir(webName)
    time_stamp = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime())
    fname = os.path.join(webName, str(getRandomInt()) + "-" + str(time_stamp) + ".png")
    print(fname)
    img = requests.get(link)
    with open(fname, 'ab') as f:
        f.write(img.content)
        f.close()
